﻿using SQLServer.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;
using Tenancy.wxpay.lib;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_WXCreateOrder 的摘要说明
    /// </summary>
    public class Hbung_WXCreateOrder : IHttpHandler
    {
        /// <summary>
        /// 微信支付调用统一下单API
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string HB_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(HB_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                Hbung_Phone = DESEncrypt.MD5Decrypt(Regex.Replace(HB_Tel.Trim(), @"\s", "+"));
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(HB_Tel) != Regex.Replace(HBsign.Trim(), @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            string Cpmstr = string.Format("select * from Hbung_CompanyInfo where CellTel='{0}'", Hbung_Phone);
            DataTable Mydata = DBHelpSQL.Query(Cpmstr).Tables[0];
            if (Mydata == null || Mydata.Rows.Count == 0)
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("用户不存在"));
                return;
            }

            //订单编号
            string Hb_OrderNum = context.Request.Params["OrderNum"];
            if (string.IsNullOrEmpty(Hb_OrderNum))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("OrderNum为空"));
                return;
            }

            string StrOrder = string.Format("select * from OnlineOrders where CPhone='{0}' and OrderNum='{1}' ", Hbung_Phone, Hb_OrderNum);
            DataTable OrderData = DbLocalSQL.Query(StrOrder).Tables[0];
            if (OrderData == null || OrderData.Rows.Count == 0)
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("订单不存在"));
                return;
            }
            string Cgsstrs = string.Format("select * from Hbung_RechargeRecords where rUserTel='{0}' and rOrderNum='{0}' ", Hbung_Phone, Hb_OrderNum);
            DataTable SXMdata = DBHelpSQL.Query(Cgsstrs).Tables[0];
            if (SXMdata != null && SXMdata.Rows.Count > 0)
            {
                if (SXMdata.Rows[0]["rStatus"].ToString() == "1")
                {
                    context.Response.Write(GenerClass.SetChangeErrorJsData("已付款"));
                    return;
                }
                else if (SXMdata.Rows[0]["rStatus"].ToString() == "0")
                {
                    string nurl = "/PayZFB/notify_url.aspx";
                    string strjson = "[{\"notufyUrl\":\"" + nurl + "\",\"tradeNo\":\"" + SXMdata.Rows[0]["tGuID"].ToString() + "\"}]";
                    context.Response.Write(GenerClass.SetSucessJsData("生成支付记录成功", strjson));
                    return;
                }
                else
                {
                    context.Response.Write(GenerClass.SetChangeErrorJsData("不能付款"));
                    return;
                }
            }
            string strOrderGuid = Guid.NewGuid().ToString(); //付费记录唯一标识

            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            string appid = WxPayConfig.APPID;// appid
            string mch_id = WxPayConfig.MCHID;//mch_id
            string nonce_str = GenerClass.GetRandomCode();//nonce_str
           
            string body = "利星行租赁-订单付款";
            string out_trade_no = strOrderGuid;//
            int total_fee = 0;                //订单总金额
            string spbill_create_ip=string.Empty; //用户端实际IP
            string notify_url = WxPayConfig.NOTIFY_URL;//异步回调地址
            string trade_type = "APP";//支付类型 

            string CGSSXMH = string.Format("appid={0}&body={3}&mch_id={1}&nonce_str={2}&out_trade_no={4&spbill_create_ip={6}}&total_fee={5}&url={7}&trade_type={8}&key={9}");
            string sign = 

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}